import {request} from "@/utils/request";
import type {AnimalDetailResult} from "@/types/animals";
import type {PageResult} from "@/types/PageResult";
import type {CommentItem, ReplyItem} from "@/types/comment";
import {CommentSortType, CommentType} from "@/enum/comment";
import {useUserStore} from "@/stores/user";
import type {UserRate} from "@/types/user";

/**
 * 根据id获取动物详细信息
 * @param ids 动物id字符串，以逗号分隔
 * @param userId 用户id
 * @returns 动物详细信息
 * @example getAnimalDetailByIdApi('1,2,3')
 */
export const getAnimalDetailByIdApi = (ids: string, userId?: number) => {
    return request<AnimalDetailResult>({
        method: 'GET',
        url: `/AW/${ids}`,
        data: {
            userId
        }
    })
}

/**
 * 收藏动物
 * @param animalID 被收藏的动物id
 * @param status 状态
 * @param type
 */
export const addFavApi = (animalID: number, status: 0 | 1, type?: 0 | 1) => {
    const typeStr=type==undefined?'':`&type=${type}`
    return request({
        method: 'POST',
        url: `/user/fav?animalID=${animalID}&status=${status}`+typeStr
    })
}


/**
 * 获取该动物的评论
 * @param data
 */
export const getCommentApi = (data: {
    page: number,
    pageSize: number,
    animalId: number,
    sortType?: CommentSortType,
    userId?: number
}) => {
    const userStore = useUserStore()
    data.sortType = data.sortType ? data.sortType : CommentSortType.LIKES
    if (userStore.userInfoData) {
        data.userId = userStore.userInfoData.userID
    }
    return request<PageResult<CommentItem[]>>({
        method: 'GET',
        url: `/comment`,
        data
    })
}

/**
 *  用户发布评论
 * @param data{animalId: string, content: string, userName: string, userAvatar: string}
 */
export const addCommentApi = (data: { animalId: number, content: string, userName: string, userAvatar: string }) => {
    return request({
        method: 'POST',
        url: '/comment/add',
        data
    })
}

/**
 *  用户发布评论
 * @param data 回复数据
 * @param data.animalId 动物id
 * @param data.content 回复内容
 * @param data.userReplyName 回复人姓名
 * @param data.userReplyAvatar 回复人头像
 * @param data.toUserId 被回复人id
 * @param data.toUserName 被回复人姓名
 * @param data.commentId 回复的评论id
 *
 */
export const addReplyApi = (data: {
    animalId: number,
    content: string,
    userReplyName: string,
    userReplyAvatar: string,
    toUserId: number,
    toUserName: string,
    commentId: number,
    type: 0 | 1,
    originId: number
}) => {
    return request({
        method: 'POST',
        url: '/comment/reply/add',
        data
    })
}


/**
 * 获取评论的回复
 * TODO 需要分页
 * @param commentId 父级评论id
 */
export const getReplyApi = (commentId: number) => {
    const userStore = useUserStore()
    return request<ReplyItem[]>({
        method: 'GET',
        url: '/comment/reply',
        data: {
            commentId,
            userId: userStore.userInfoData?.userID
        }
    })
}

/**
 * 点赞
 * @param data
 * commentId-评论id,
 * type-评论类型
 * status-点赞状态
 * 0-取消点赞，1-点赞
 */
export const handleLikedApi = (data: { commentId: number, type: CommentType, status: 0 | 1, likedUserId: number }) => {
    return request({
        method: 'POST',
        url: '/comment/like',
        data
    })
}

/**
 * 点踩
 * commentId-评论id,
 *  status-点踩状态
 * 0-取消点踩，1-点踩
 * @param commentId
 * @param status
 * @param userId
 */
export const handleDislikeApi = (commentId: number, status: 0 | 1, userId: number) => {
    return request({
        method: 'POST',
        url: `/comment/dislike?commentId=${commentId}&status=${status}&dislikedUserId=${userId}`,
    })
}

/**
 *  删除评论
 * @param commentId 评论id
 */
export const handleDeleteApi = (commentId: number) => {
    return request({
        method: 'POST',
        url: '/comment/delete?commentId=' + commentId,
    })
}
export const handleDeleteReplyApi = (replyId: number) => {
    return request({
        method: 'POST',
        url: '/comment/delete/reply?replyId=' + replyId,
    })
}

/**
 *  举报评论
 * @param commentId 评论id
 */
export const handleReportApi = (commentId: number) => {
    return request({
        method: 'POST',
        url: '/AW/comment/report',
        data: {
            commentId
        }
    })
}

/**
 * 评分
 * @param animalId 动物id
 * @param score 评分
 */
export const ratingApi = (animalId: number, score: number) => {
    return request<UserRate>({
        method: 'POST',
        url: '/user/rate?animalId=' + animalId + '&score=' + score
    })
}